#include "gnlUserInterface.h"


 void TW_CALL showLightBar(void *clientData){


}



 void  gnlUserInterface::sceneBar(float *rotation, float *translation, float *bgColor){
	
	
	sceneSetup = TwNewBar ("Scene Setup");
	
	//Create visual evironment
	TwDefine(" GLOBAL help='Setup all scene objects!' "); // Message added to the help bar.
    TwDefine(" TweakBar size='500 400' color='0 0 5'"); // change default tweak bar size and color

	TwAddVarRW(sceneSetup, "Cor de fundo", TW_TYPE_COLOR3F, bgColor, "step=1");
	TwAddVarRW(sceneSetup, "RotacaoX", TW_TYPE_FLOAT, &rotation[0], 
               " max=360 step=2 keyIncr=z keyDecr=Z group='Rotate' ");
	TwAddVarRW(sceneSetup, "RotacaoY", TW_TYPE_FLOAT, &rotation[1], 
               " max=360 step=2 keyIncr=z keyDecr=Z group='Rotate' ");
	
	TwAddVarRW(sceneSetup, "RotacaoZ", TW_TYPE_FLOAT, &rotation[2], 
               " max=360 step=2 keyIncr=z keyDecr=Z group='Rotate' ");
			   
	

	//TwAddVarRW(sceneSetup, "ObjRotation", TW_TYPE_QUAT4F, &g_Rotation, 
     //          " label='Object rotation' open help='Change the object orientation.' ");
	TwAddVarRW(sceneSetup, "TranslacaoX", TW_TYPE_FLOAT, &translation[0], 
               " max=360 step=0.2 keyIncr=z keyDecr=Z  group='Translate'");

	TwAddVarRW(sceneSetup, "TranslacaoY", TW_TYPE_FLOAT, &translation[1], 
               " max=360 step=0.2 keyIncr=z keyDecr=Z  group='Translate'");

	TwAddVarRW(sceneSetup, "TranslacaoZ", TW_TYPE_FLOAT, &translation[2], 
               " max=360 step=0.2 keyIncr=z keyDecr=Z  group='Translate'");

	TwAddSeparator(sceneSetup, "sep1", NULL);
// 
	TwAddButton(sceneSetup, "SVAL", showLightBar, NULL, " label='Save all states' ");


}


 void gnlUserInterface::materialBar(gnlMaterial *mat){
	materialSetup = TwNewBar("Material Setup");                 
    TwDefine(" Lights label='MaterialBar' size='200 800' position='580 600' alpha=0 color='0 0 5' help='Use this bar to edit the lights in the scene.' ");

	TwAddVarRW(materialSetup, "Ambient", TW_TYPE_COLOR3F, mat->getAmbientReflectance(), " group='Reflectance' ");
	TwAddVarRW(materialSetup, "Diffuse", TW_TYPE_COLOR3F, mat->getDiffuseReflectance(), " group='Reflectance' ");
	TwAddVarRW(materialSetup, "Specular", TW_TYPE_COLOR3F, mat->getSpecularReflectance(), " group='Reflectance'");
	TwAddVarRW(materialSetup, "Emissive", TW_TYPE_COLOR3F, mat->getEmissiveReflectance(), " group='Reflectance'");
	TwAddVarRW(materialSetup, "Shininess", TW_TYPE_FLOAT, mat->getShininess(), " ");
	

 }


 void gnlUserInterface::lightBar(gnlSpotLight *spot){

	lightSetup = TwNewBar("Lights Setup");                 
    TwDefine(" Lights label='Lights Bar' size='200 800' position='580 600' alpha=0 color='0 0 5' help='Use this bar to edit the lights in the scene.' ");
	
	//Direction
	TwAddVarRW(lightSetup, "LightDir", TW_TYPE_DIR3F, spot->getDirection(), 
               " label='Light direction' open help='Change the light direction.' ");

	TwAddVarRW(lightSetup, "Quadratic", TW_TYPE_FLOAT, spot->getQuadraticAttenuation(), 
               "step=0.1 keyIncr=z keyDecr=Z  group='Attenuation'");
	
	TwAddVarRW(lightSetup, "Linear", TW_TYPE_FLOAT, spot->getLinearAttenuation(), 
               "step=0.1 keyIncr=z keyDecr=Z  group='Attenuation'");
	
	TwAddVarRW(lightSetup, "Constant", TW_TYPE_FLOAT, spot->getConstantAttenuation(), 
               "step=0.1 keyIncr=z keyDecr=Z  group='Attenuation'");

	//Position
	TwAddVarRW(lightSetup, "X", TW_TYPE_FLOAT, (spot->getPosition()), 
               "step=0.5 group='Position'");
	
	TwAddVarRW(lightSetup, "Y", TW_TYPE_FLOAT, (spot->getPosition()+1), 
               "step=0.5 group='Position'");
	
	TwAddVarRW(lightSetup, "Z", TW_TYPE_FLOAT, (spot->getPosition()+2), 
               "step=0.5  group='Position'");


	//Light Colors
	TwAddVarRW(lightSetup, "Ambient", TW_TYPE_COLOR3F, spot->getAmbientColor(), " group='Light Colors' step=1");
	TwAddVarRW(lightSetup, "Diffuse", TW_TYPE_COLOR3F, spot->getDiffuseColor(), " group='Light Colors' step=1");
	TwAddVarRW(lightSetup, "Specular", TW_TYPE_COLOR3F, spot->getSpecularColor(), " group='Light Colors' step=1");
	
	//
	TwAddVarRW(lightSetup, "CutoffAngle", TW_TYPE_FLOAT, spot->getSpotCutOff(), 
               "min=0 max=180 step=0.5 ");
	TwAddVarRW(lightSetup, "SpotExponent", TW_TYPE_FLOAT, spot->getSpotExponent(), 
               "step=0.5 ");
	/*
	int NumLights=0;
	TwAddVarRW(lightSetup, "NumLights", TW_TYPE_INT32, &NumLights, 
               " label='Number of lights' keyIncr=l keyDecr=L help='Changes the number of lights in the scene.' ");

	int zero = 0;
	int maxLights = 8;
    TwSetParam(lightSetup, "NumLights", "min", TW_PARAM_INT32, 1, &zero);
    TwSetParam(lightSetup, "NumLights", "max", TW_PARAM_INT32, 1, &maxLights);
*/


}


void gnlUserInterface::create(float *rotation, float *translation, float *bgColor, gnlSpotLight *spotLight, gnlMaterial *mat){

	TwInit(TW_OPENGL, NULL);
	sceneBar(rotation, translation, bgColor);
	lightBar(spotLight);
	materialBar(mat);

		
	// Set GLUT event callbacks
    // - Directly redirect GLUT mouse button events to AntTweakBar
    glutMouseFunc((GLUTmousebuttonfun)TwEventMouseButtonGLUT);
    // - Directly redirect GLUT mouse motion events to AntTweakBar
    glutMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
    // - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion)
    glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
    // - Directly redirect GLUT key events to AntTweakBar
    glutKeyboardFunc((GLUTkeyboardfun)TwEventKeyboardGLUT);
    // - Directly redirect GLUT special key events to AntTweakBar
    glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT);
    // - Send 'glutGetModifers' function pointer to AntTweakBar;
    //   required because the GLUT key event functions do not report key modifiers states.
    TwGLUTModifiersFunc(glutGetModifiers);


}


void gnlUserInterface::render(){
	TwDraw();
}